package me.exchange.com.modules.system.controller;

import me.exchange.com.aop.log.Log;
import me.exchange.com.utils.Result;
import me.exchange.com.modules.system.domain.FileContent;
import me.exchange.com.modules.system.service.FileContentService;
import me.exchange.com.modules.system.dto.FileContentDto;
import me.exchange.com.modules.system.dto.FileContentQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author zjj
* @date 2020-05-14
*/
@Api(tags = "云文件存储管理")
@RestController
@RequestMapping("/api/fileContent")
public class FileContentController {

    private final FileContentService fileContentService;

    public FileContentController(FileContentService fileContentService) {
        this.fileContentService = fileContentService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @PostMapping(value = "/list/download")
    @PreAuthorize("@el.check('fileContent:list')")
    public void download(HttpServletResponse response, FileContentQueryCriteria criteria) throws IOException {
        fileContentService.download(fileContentService.queryAll(criteria), response);
    }

    @PostMapping("/list")
    @Log("查询云文件存储")
    @ApiOperation("查询云文件存储")
    @PreAuthorize("@el.check('fileContent:list')")
    public ResponseEntity<Object> getFileContents(FileContentQueryCriteria criteria, Pageable pageable){
        Object fileContent =  fileContentService.queryAll(criteria,pageable);
        return new ResponseEntity<>(Result.ok("云文件存储查询成功").put("fileContentList",fileContent),HttpStatus.OK);
    }

    @PostMapping("/queryInfo")
    @Log("查询云文件存储详细")
    @ApiOperation("查询云文件存储详细")
    @PreAuthorize("@el.check('fileContent:list')")
    public ResponseEntity<Object> queryInfo(Long id){
        FileContentDto fileContent =  fileContentService.findById(id);
        return new ResponseEntity<>(Result.ok("云文件存储详请查询成功").put("fileContent",fileContent),HttpStatus.OK);
    }

    @PostMapping("/add")
    @Log(value="新增云文件存储",code="")
    @ApiOperation("新增云文件存储")
    @PreAuthorize("@el.check('fileContent:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody FileContent resources){
        fileContentService.create(resources);
        return new ResponseEntity<>(Result.ok("云文件存储新增成功"),HttpStatus.OK);
    }

    @PostMapping("/update")
    @Log(value="修改云文件存储",code="")
    @ApiOperation("修改云文件存储")
    @PreAuthorize("@el.check('fileContent:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody FileContent resources){
        fileContentService.update(resources);
        return new ResponseEntity<>(Result.ok("云文件存储修改成功"),HttpStatus.OK);
    }

    @Log(value="删除云文件存储",code="")
    @ApiOperation("删除云文件存储")
    @PreAuthorize("@el.check('fileContent:del')")
    @PostMapping("/del")
    public ResponseEntity<Object> delete(@RequestBody FileContentQueryCriteria criteria) {
        fileContentService.deleteAll(criteria.getIds());
        return new ResponseEntity<>(Result.ok("云文件存储删除成功"),HttpStatus.OK);
    }
}